package middleware

import (
	"bytes"
	"fmt"
	"github.com/lzppppppp/virapaksa-go/internal/system_monitor/model"
	"log"
	"net/http"
	"strings"
)

/*
 * Http GET方法，主要封装报错处理
 * 输入 FuncVar, 返回*http.Response
 */
func GetData(url model.FuncVar) *http.Response {

	var targetUrlB strings.Builder
	targetUrlB.WriteString("http://")
	targetUrlB.WriteString(url.IP)
	targetUrlB.WriteString(":")
	targetUrlB.WriteString(url.Port)
	targetUrlB.WriteString(url.API)

	targetUrl := targetUrlB.String()
	fmt.Println(targetUrl)

	req, err := http.NewRequest("GET", targetUrl, nil)

	if err != nil {
		log.Printf("ERROR: Create HTTP POST request failed %s \n", targetUrl)
		panic(err)
	}

	// defer req.Body.Close()
	// basic auth 认证
	if url.Token != "" {
		req.Header.Set("Authorization", "Bearer "+url.Token)
	} else if (url.User != "") && (url.Password != "") {
		req.SetBasicAuth(url.User, url.Password)
	}

	response, err := http.DefaultClient.Do(req)

	if err != nil {
		log.Printf("ERROR: Send HTTP POST request failed %s \n", targetUrl)
		panic(err)
	}

	return response
}

/*
 * Http POST方法，主要封装报错处理
 * 输入 FuncVar和json数据的[]byte类型, 返回*http.Response
 */
func PostData(url model.FuncVar, d []byte) *http.Response {

	var targetUrlB strings.Builder
	targetUrlB.WriteString("http://")
	targetUrlB.WriteString(url.IP)
	targetUrlB.WriteString(":")
	targetUrlB.WriteString(url.Port)
	targetUrlB.WriteString(url.API)
	// 目标url
	// targetUrl := "http://" + url.IP + ":" + url.Port + url.API
	targetUrl := targetUrlB.String()

	fmt.Println(targetUrl)
	log.Printf("INFO: POST target %s \n", targetUrl)

	req, err := http.NewRequest("POST", targetUrl, bytes.NewBuffer(d))
	if err != nil {
		log.Printf("ERROR: Create HTTP POST request failed %s \n", targetUrl)
		panic(err)
	}
	// 必须设定该参数,POST参数才能正常提交
	defer req.Body.Close()
	// 请求头，意思是以json串提交数据
	req.Header.Set("Content-Type", "application/json;charset=UTF-8")

	// basic auth 认证
	if url.Token != "" {
		req.Header.Set("Authorization", "Bearer "+url.Token)
	} else if (url.User != "") && (url.Password != "") {
		req.SetBasicAuth(url.User, url.Password)
	}

	// 响应
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Printf("ERROR: Send HTTP POST request failed %s \n", targetUrl)
		panic(err)
	}

	return response
}

/*
 * Http PUT方法，主要封装报错处理
 * 输入 FuncVar和json数据的[]byte类型, 返回*http.Response
 */
func PutData(url model.FuncVar, d []byte) *http.Response {

	// 带header的post方法
	targetUrl := "http://" + url.IP + ":" + url.Port + url.API
	//fmt.Println(targetUrl)
	log.Printf("INFO: PUT TARGET: %s \n", targetUrl)
	//fmt.Println("target:", targetUrl)
	data := strings.NewReader(string(d))

	var req *http.Request

	if data == nil {
		req, _ = http.NewRequest("PUT", targetUrl, nil)
	} else {
		req, _ = http.NewRequest("PUT", targetUrl, data)
	}

	req.Header.Set("Content-Type", "application/json;charset=UTF-8")

	// basic auth 认证
	if url.Token != "" {
		req.Header.Set("Authorization", "Bearer "+url.Token)
	} else if (url.User != "") && (url.Password != "") {
		req.SetBasicAuth(url.User, url.Password)
	}

	// 读取post返回的数据
	response, _ := http.DefaultClient.Do(req)

	return response

}
